package com.travel.common.persitence;

import java.io.Serializable;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;

import com.travel.common.util.AbstractBean;

public class DaoUtil extends AbstractBean {
	private static final long serialVersionUID = 901598089718591950L;

	public Object saveOrUpdate(Object item) {
		HibernateUtil beanHibernateUtil = (HibernateUtil) getBean("beanHibernateUtil");
		Session session = beanHibernateUtil.getSession();
		try {
			session.saveOrUpdate(item);
			session.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			beanHibernateUtil.closeSession(session);
		}

		return item;
	}

	@SuppressWarnings("rawtypes")
	public List<?> list(String model) {
		HibernateUtil beanHibernateUtil = (HibernateUtil) getBean("beanHibernateUtil");

		List items = null;

		Session session = beanHibernateUtil.getSession();
		try {
			session.beginTransaction();
			items = session.createQuery("from " + model).list();
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			beanHibernateUtil.closeSession(session);
		}

		return items;
	}

	public Object add(Object item) throws Exception {
		HibernateUtil beanHibernateUtil = (HibernateUtil) getBean("beanHibernateUtil");

		Session session = beanHibernateUtil.getSession();
		try {
			session.beginTransaction();
			session.save(item);
			session.flush();
			session.getTransaction().commit();
		} catch (Exception localException) {
		} finally {
			beanHibernateUtil.closeSession(session);
		}

		return item;
	}

	public Object edit(Object item) throws Exception {
		HibernateUtil beanHibernateUtil = (HibernateUtil) getBean("beanHibernateUtil");

		Session session = beanHibernateUtil.getSession();
		try {
			session.beginTransaction();
			session.update(item);
			session.flush();

			session.getTransaction().commit();
		} catch (Exception localException) {
		} finally {
			beanHibernateUtil.closeSession(session);
		}

		return item;
	}

	public Object delete(Object item) throws Exception {
		HibernateUtil beanHibernateUtil = (HibernateUtil) getBean("beanHibernateUtil");

		Session session = beanHibernateUtil.getSession();
		try {
			session.beginTransaction();
			session.delete(item);
			session.flush();
			session.getTransaction().commit();
		} catch (Exception localException) {
		} finally {
			beanHibernateUtil.closeSession(session);
		}

		return item;
	}

	public void deleteAll(String model) throws Exception {
		HibernateUtil beanHibernateUtil = (HibernateUtil) getBean("beanHibernateUtil");

		Session session = beanHibernateUtil.getSession();
		try {
			session.beginTransaction();
			String hql = "delete from " + model;
			Query query = session.createQuery(hql);
			query.executeUpdate();
			session.flush();
			session.getTransaction().commit();
		} catch (Exception localException) {
		} finally {
			beanHibernateUtil.closeSession(session);
		}
	}

	@SuppressWarnings("rawtypes")
	public List<?> query(Class modelClass, List<Criterion> criterions) {
		List items = null;
		HibernateUtil beanHibernateUtil = (HibernateUtil) getBean("beanHibernateUtil");
		Session session = beanHibernateUtil.getSession();
		try {
			session.beginTransaction();
			Criteria criteria = session.createCriteria(modelClass);
			if (criterions != null) {
				for (int i = 0; i < criterions.size(); i++) {
					Criterion criterion = (Criterion) criterions.get(i);
					criteria.add(criterion);
				}
			}
			items = criteria.list();
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			beanHibernateUtil.closeSession(session);
		}
		return items;
	}

	@SuppressWarnings("rawtypes")
	public Object get(Class cls, Serializable id) {
		Object obj = null;
		HibernateUtil beanHibernateUtil = (HibernateUtil) getBean("beanHibernateUtil");
		Session session = beanHibernateUtil.getSession();
		try {
			session.beginTransaction();

			obj = session.get(cls, id);
			session.getTransaction().commit();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			beanHibernateUtil.closeSession(session);
		}
		return obj;
	}
}